package com.sba.core.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 角色.
 * 
 * @author feiliming
 *
 */
@Entity(name = "sys_role")
public class Role implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id;
	private String name;
	private String code;
	
	@Temporal(value = TemporalType.DATE)
	private Date createTime;
	private int sort;
	//private int isEnabled;
	private int isSys;
	private String description;
	
	//1.单向关联, Menu中没有Role
	//2.双向关联, Menu中有Role, 如果使用双向可直接判断菜单是否被角色使用
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "sys_role_menu", 
		joinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}, 
		inverseJoinColumns = {@JoinColumn(name = "menu_id", referencedColumnName = "id")})
	@OrderBy("sort DESC")
	private Set<Menu> menus = new HashSet<Menu>();
	
	//双向关联, 用mappedBy, 被维护
	@ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
	private Set<User> users = new HashSet<User>();
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public int getSort() {
		return sort;
	}
	public void setSort(int sort) {
		this.sort = sort;
	}
	public int getIsSys() {
		return isSys;
	}
	public void setIsSys(int isSys) {
		this.isSys = isSys;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Set<Menu> getMenus() {
		return menus;
	}
	public void setMenus(Set<Menu> menus) {
		this.menus = menus;
	}
	
	public Set<User> getUsers() {
		return users;
	}
	@JsonIgnore
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
}
